import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:my_app/app/core/utils/image_util.dart';
import 'package:my_app/app/core/utils/object_util.dart';
import 'package:my_app/app/core/theme/app_text_theme.dart';
import 'package:my_app/app/widgets/custom_image_widget.dart';
import 'package:my_app/app/widgets/picker/custom_image_picker_widget.dart';

import 'image_tool_controller.dart';

class ImageToolPage extends GetView<ImageToolController> {
  final CustomImagePickerWidgetController imageController =
      CustomImagePickerWidgetController();

  @override
  Widget build(BuildContext context) {
    Get.put(ImageToolController());
    return Scaffold(
      appBar: AppBar(title: Text('图片工具')),
      body: ListView(
        padding: EdgeInsets.all(10.0),
        children: [
          _imagePickerWidget(),
          SizedBox(height: 20.0),
          _compressDemo(),
          SizedBox(height: 20.0),
          _watermarkDemo(),
        ],
      ),
    );
  }

  Widget _imagePickerWidget() {
    return Column(
      children: [
        CustomImagePickerWidget(
          controller: imageController,
          maxCount: 1,
        ),
        SizedBox(height: 10.0),
        Text('选择或拍摄一张图片', style: tips),
      ],
    );
  }

  ///图片压缩示例
  Widget _compressDemo() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          children: [
            Text(
              '图片压缩',
              style: cardTitle,
            ),
            SizedBox(height: 10.0),
            Obx(
              () => Column(
                children: [
                  Text('压缩前文件大小：${controller.beforeCompressFileSize}MB'),
                  Text('压缩后文件大小：${controller.afterCompressFileSize}MB')
                ],
              ),
            ),
            ElevatedButton(
              onPressed: () async {
                await getSelectedImages();
                if (!ObjectUtil.isEmpty(controller.beforeCompressFile.value)) {
                  File? result = await compressImageFileAndGetFile(
                      controller.beforeCompressFile.value!);
                  if (result != null) {
                    controller.changeAfterCompressFile(result);
                  }
                }
              },
              child: Text('立即压缩'),
            )
          ],
        ),
      ),
    );
  }

  getSelectedImages() async {
    List list = await imageController.getSelectedImages();
    if (ObjectUtil.isEmptyList(list)) {
      SmartDialog.showNotify(msg: "请选择图片", notifyType: NotifyType.failure);
      return;
    }
    File file = list[1][0];
    controller.changeBeforeCompressFile(file);
  }

  ///图片添加水印示例
  Widget _watermarkDemo() {
    return Card(
      child: Padding(
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: [
            Text(
              '图片水印',
              style: cardTitle,
            ),
            SizedBox(height: 10.0),
            TextField(
              decoration: InputDecoration(
                hintText: '输入水印内容',
                labelText: "水印内容",
                contentPadding:
                    EdgeInsets.symmetric(vertical: 8.0, horizontal: 8.0),
                border: OutlineInputBorder(),
              ),
              onChanged: (value) => controller.changeWatermark(value),
            ),
            SizedBox(height: 10.0),
            Obx(() => controller.watermarkFile.value != null
                ? CustomImageWidget(
                    height: 150.0,
                    imageUrl: controller.watermarkFile.value!.path)
                : Container()),
            SizedBox(height: 10.0),
            ElevatedButton(
                onPressed: () async {
                  await getSelectedImages();
                  if (!ObjectUtil.isEmpty(
                      controller.beforeCompressFile.value)) {
                    SmartDialog.showLoading(msg: '正在添加水印');
                    File? watermarkFile = await fileTypeImageWatermark(
                        controller.beforeCompressFile.value!,
                        controller.watermark.value);
                    SmartDialog.dismiss();
                    controller.changeWatermarkFile(watermarkFile);
                  }
                },
                child: Text('添加水印'))
          ],
        ),
      ),
    );
  }
}
